var express = require('express');
var superagent = require('superagent');
var router = express.Router();

// 三方登录*******************************************************
router.post('/ajax/wxlogin', function(req, res, next) {
  console.log('三方登录');
  superagent
    .post(global.baseURL + '/api/users/social/login')
    .send({
      openId: req.body.openId,
      unionId: req.body.unionId
    })
    .set('DEVICE', 'weixin')
    .end(function(err1, res1) {
      if (res1 != null && res1.ok) {
        res.send(res1.body);

      }
      else {
        console.error('请求数据错误! ');
        logger.error('请求数据错误。');
      }
    });
});

// 三方注册*******************************************************
router.post('/ajax/wxregister', function(req, res, next) {
  console.log('三方注册');
  superagent
    .post(global.baseURL + '/api/users/social/register')
    .send({
      nickName: req.body.nickName,
      avatar: req.body.avatar,
      sex: req.body.sex,
      openId: req.body.openId,
      unionId: req.body.unionId
    })
    .set('DEVICE', 'weixin')
    .end(function(err1, res1) {
      if (res1 !== undefined && res1.ok) {
        res.send(res1.body);
      }
      else {
        console.error('请求数据错误! ');
        logger.error('请求数据错误。');
      }
    });
});

// 三方绑定*******************************************************
router.post('/ajax/bind', function(req, res, next) {
  console.log('三方绑定');

	superagent
		.post(global.baseURL + '/api/users/social/bind')
		.send({
			nickName: req.body.nickName,
			avatar: req.body.avatar,
			sex: req.body.sex,
			unionId: req.body.unionId,
			email:req.body.email,
			password:req.body.password
		})
		.set('DEVICE', 'weixin')

		.end(function(err1, res1) {
			if (res1 !== undefined && res1.ok) {
				res.send(res1.body);
				console.log('绑定接口ok');
			}
			else {
				console.error('请求数据错误! ');
				logger.error('请求数据错误。');
			}
		});
});


// 三方注册，有邮箱*******************************************************
router.post('/ajax/wxregister2', function(req, res, next) {
  console.log('三方注册，有邮箱');

	superagent
		.post(global.baseURL + '/api/users/social/register')
		.send({
			nickName: req.body.nickName,
			avatar: req.body.avatar,
			sex: req.body.sex,
			unionId: req.body.unionId,
			email:req.body.email,
			password:req.body.password
		})
		.set('DEVICE', 'weixin')

		.end(function(err1, res1) {
			if (res1 !== undefined && res1.ok) {
				res.send(res1.body);
				console.log('三方注册（有邮箱）接口ok');
			}
			else {
				console.error('请求数据错误! ');
				logger.error('请求数据错误。');
			}
		});
});

// 邮箱注册*******************************************************
router.post('/ajax/emailRegister', function(req, res, next) {
  console.log('邮箱注册');
	superagent
		.post(global.baseURL + '/api/users/register')
		.send({
			nickName: req.body.nickName,
			email:req.body.email,
			password:req.body.password
		})
		.set('DEVICE', 'weixin')

		.end(function(err1, res1) {
			if (res1 !== undefined && res1.ok) {
				res.send(res1.body);
				console.log('邮箱注册接口ok');
			}
			else {
				console.error('请求数据错误! ');
				logger.error('请求数据错误。');
			}
		});

});

// 核对邮箱*******************************************************
router.post('/ajax/checkEmail', function(req, res, next) {
  console.log('邮箱验证');
	superagent
		.post(global.baseURL + '/api/users/checkEmail')
		.send({
			email:req.body.email,
		})
		.set('DEVICE', 'weixin')
		.end(function(err1, res1) {
			if (res1 !== undefined && res1.ok) {
				res.send(res1.body);
			}
			else {
				console.error('请求数据错误! ');
				logger.error('请求数据错误。');
			}
		});

});


module.exports = router;
